package com.ysx.admin.job;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.ysx.admin.service.impl.AdWemediaNewsAutoScanServiceImpl;
import com.ysx.common.utils.RequestHeaderInfoUtils;
import com.ysx.wemedia.feign.WeMediaFeign;
import com.ysx.wemedia.feign.WeMediaNewsFeign;
import com.ysx.wemedia.pojo.WmNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

/**
 * 扫描定时发布素材
 */
@Component
public class ScanTask {

    @Autowired
    private WeMediaNewsFeign weChatMediaNewsFeign;//feign自媒体微服务

    @Autowired
    private AdWemediaNewsAutoScanServiceImpl adWemediaNewsAutoScanServiceImpl;//Ad自媒体微服务快捷调用


    @XxlJob("Scan_WmNewsJob")
    public ReturnT<String> stringReturnTcanWmNewsJob() {

        //调用自媒体微服务 查询待发布的素材(8)
        List<WmNews> newsListByStatus = weChatMediaNewsFeign.getNewsListByStatus(8);

        //校验参数合法性
        if (null==newsListByStatus) {
            return ReturnT.FAIL;
        }

        Iterator<WmNews> iterator = newsListByStatus.iterator();
        while (iterator.hasNext()) {
            WmNews wmNews=  iterator.next();
            //获取发布时间
            LocalDateTime publishTime = wmNews.getPublishTime();
            //发布时间为null 既为人工审核 直接发布文章
            if (null == publishTime) {
                //更改自媒体信息并更新
                wmNews.setStatus(9);
                weChatMediaNewsFeign.updateByPrimaryKey(wmNews);
                //发布文章 调用抽取好的方法
                adWemediaNewsAutoScanServiceImpl.AdPublishNewsToArticleService(wmNews);
            }
            //不为null
            else {
                //获取当前时间
                LocalDateTime now = LocalDateTime.now();
                //判断发布时间是否大于等于当前时间
                if (publishTime.isAfter(now) || publishTime.equals(now)) {
                    wmNews.setStatus(9);

                    weChatMediaNewsFeign.updateByPrimaryKey(wmNews);

                    //发布文章
                    adWemediaNewsAutoScanServiceImpl.AdPublishNewsToArticleService(wmNews);
                } else {
                    System.out.println("发布时间还没到...");
                }
            }
        }

        //任务响应状态
        return ReturnT.SUCCESS;
    }


}
